/* 
 * Gneat (http://gneat.org)
 *
 * (C) Copyright 2011 and beyond - Claudio Stella, all rights reserved.
 *
 * This file is part of Gneat.
 *
 * Gneat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL)
 * as published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gneat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * and the GNU General Public License, for completeness, along with Gneat.  
 * If not, see <http://www.gnu.org/licenses/>.
 */
package org.neatshell.gneat.client.handler;

import java.util.Iterator;
import java.util.List;

import org.neatshell.gneat.client.event.live.BlurEvent;
import org.neatshell.gneat.client.event.live.CustomEvent;
import org.neatshell.gneat.client.event.live.SubmitEvent;
import org.neatshell.gneat.client.event.live.mouse.ClickEvent;
import org.neatshell.gneat.client.event.live.mouse.MouseOutEvent;
import org.neatshell.gneat.client.event.live.mouse.MouseOverEvent;
import org.neatshell.gneat.client.handler.mouse.ClickHandler;
import org.neatshell.gneat.client.handler.mouse.MouseOutHandler;
import org.neatshell.gneat.client.handler.mouse.MouseOverHandler;

import com.google.gwt.user.client.ui.Widget;

public class Handlers {
	public static void register(Handler handler, Widget widget) {
		if (handler instanceof BlurHandler) {
			handler._registration = widget.addDomHandler((BlurHandler)  handler, BlurEvent.getType());
		}
		else if (handler instanceof CustomHandler) {
			handler._registration = widget.addHandler((CustomHandler) handler, CustomEvent.getType());
		}
		else if (handler instanceof ClickHandler) {
			handler._registration = widget.addDomHandler((ClickHandler)  handler, ClickEvent.getType());
		}
		else if (handler instanceof MouseOutHandler) {
			handler._registration = widget.addDomHandler((MouseOutHandler)  handler, MouseOutEvent.getType());
		}
		else if (handler instanceof MouseOverHandler) {
			handler._registration = widget.addDomHandler((MouseOverHandler)  handler, MouseOverEvent.getType());
		}
		else if (handler instanceof SubmitHandler) {
			handler._registration = widget.addDomHandler((SubmitHandler) handler, SubmitEvent.getType());
		}
	}

	public static void unregister(List<Handler> handlers) {
		if (handlers != null) {
			Iterator<Handler> it_handlers = handlers.iterator();
			while (it_handlers.hasNext()) {
				Handler handler = (Handler) it_handlers.next();
				handler._registration.removeHandler();
			}
			handlers.clear();
		}
	}
}